// previously was
// import Router from 'vue-router'
import {
	createRouter,
	createWebHashHistory
} from 'vue-router'
import Login from './pages/Login.vue'
import Home from './pages/Home.vue'
import CounterBid from './pages/CounterBid.vue'
import Root from './pages/Root.vue'
import Storage from './util/storage.js';
import Theme from './config/theme.js';
// 2. 定义一些路由
// 每个路由都需要映射到一个组件。

const routes = [
	{
		path: '/',
		component: Root,
		name: 'Root',
	},
	{
		path: '/login',
		component: Login,
		name: 'Login',
	},
	{
		path: '/home',
		component: Home,
		name: 'Home',
	},
	{
		path: '/counterBid',
		component: CounterBid,
		name: 'CounterBid',
	}

]

function isAuthenticated() {
	var userInfo = Storage.getUserInfo();
	if (userInfo != undefined && userInfo.success != undefined && userInfo.success == true) {
		return true;
	}
	return false;

}

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
	// 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
	history: createWebHashHistory(),
	routes, // `routes: routes` 的缩写
})

router.beforeEach((to, from, next) => {
  let theme =	localStorage.getItem("theme");
  if(theme != null){
	  next()
	  return;
  }
  
  /* 路由发生变化修改页面title */
  let pathname = window.location.pathname.replaceAll("/", "");
  
  let loginTheme = Theme['yxy'].login;
  localStorage.setItem("theme",'yxy');
  
 
  if (pathname != null && pathname.length > 0 &&  Theme[pathname] != undefined) {
	loginTheme = Theme[pathname].login;
	localStorage.setItem("theme",pathname);
  }
  
  
  if (loginTheme.name) {
     document.title = loginTheme.name
	 localStorage.setItem("themeName",loginTheme.name);
  }
  next()
})

// BAD
// router.beforeEach((to, from, next) => {
// 	if (to.name !== 'Login' && to.name !== 'Auth' && !isAuthenticated()) {
// 		next({
// 			name: 'Login'
// 		})
// 	} else {
// 		next()
// 	}
// })

export default {
	router
}
